<!--
 * @Author: your name
 * @Date: 2021-06-27 14:11:45
 * @LastEditTime: 2021-06-27 21:11:17
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue_shop_mananger\src\views\login\login.vue
-->
<template>
  <div class="login_container">
    <div class="login_box">
      <div class="img_box">
        <img src="../../assets/img/logo.png" alt="">
      </div>
      <el-form ref="loginResetRef" class="form-box" :model="login_form" :rules="loginRules">
        <el-form-item prop="username" >
          <el-input prefix-icon="el-icon-user-solid" type="text" v-model="login_form.username"></el-input>
        </el-form-item>
        <el-form-item  prop="password" >
          <el-input prefix-icon="el-icon-lock" type="password" v-model="login_form.password"></el-input>
        </el-form-item>
      </el-form>
      <div class="btn_box">
        <el-button @click="login" type="success">登录</el-button>
        <el-button @click="loginResetForm">重置</el-button>
      </div>
    </div>
  </div>
</template>
<script>
  export default {
    data(){
      return {
        login_form:{
          username:'admin',
          password:'123456'
        },
        //表单认证规则
        loginRules:{
          //认证账号
          username:[
           { required: true, message: '请输入用户名', trigger: 'blur' },
           { min: 5,max:11, message: '请输入5-11的字符', trigger: 'blur' }
          ],
          //认证密码
          password:[
           { required: true, message: '请输入密码', trigger: 'blur' },
           { min: 5,max:11, message: '请输入5-11的字符', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      loginResetForm(){
        this.$refs.loginResetRef.resetFields();
      },
      login(){
        this.$refs.loginResetRef.validate(async (valid)=>{
          if(valid){
            const {data:res}=await this.$http.post('login',this.login_form);
            if (res.meta.status!==200) return this.$message.error('登录失败！');
            this.$message.success('登录成功！');
            //console.log(res);
            window.sessionStorage.setItem('token',res.data.token);
            this.$router.push('/home');
          }else{
            return;
          }
        })
      }
    },

  }
</script>
<style lang="scss" scoped>
  .login_container {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background-color: #2b4b6b;
    .login_box {
      width: 450px;
      height: 320px;
      border-radius: 5px;
      background-color: #fff; 
      .img_box {
        width: 130px;
        height: 130px;
        border-radius: 50%;
        border: 1px solid #eee;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        background-color: #fff;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
          border: 1px solid #eee;
          background-color: #eee;
        }
      }
    }
    .form-box{
      margin-top: 20%;
      padding: 20px;
    }
    .btn_box{
      display: flex;
      justify-content: flex-end;
      padding: 0 20px;
    }
  }
</style>